/*
 * CrearUser.java
 *
 * Created on 24 de julio de 2007, 10:45 AM
 */

package administrador;
import javax.swing.*;
import java.sql.*;
/**
 *
 * @author  nexxus
 */
public class CrearBodega extends javax.swing.JFrame {
    Conexion conexion;
    BodegaAdmin uadmin;
    /** Creates new form CrearUser */
    public CrearBodega(Conexion c,BodegaAdmin ua) {
        uadmin=ua;
        conexion=c;
        initComponents();
        nuevoCodigo();
        this.setSize(313,231);
        this.setVisible(true);
        bt_crear.setText("Insertar");
        bt_borrar.setVisible(false);
    }
    public CrearBodega(Conexion c,BodegaAdmin ua, int fila) {
        uadmin=ua;
        conexion=c;
        initComponents();
        this.setSize(370,350);
        this.setVisible(true);
        cargarDatos(fila);
        bt_crear.setText("Modificar");
        bt_borrar.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_nom = new javax.swing.JTextField();
        bt_crear = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        lbl_cod = new javax.swing.JLabel();
        bt_borrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txt_obs = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Crear Bodega"));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setForeground(new java.awt.Color(102, 0, 0));
        jLabel1.setText("Nuevo Bodega");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(30, 10, 130, 40);

        jLabel2.setText("Nombre:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(60, 80, 50, 20);

        jPanel1.add(txt_nom);
        txt_nom.setBounds(110, 80, 160, 20);

        bt_crear.setText("Crear Categoria");
        bt_crear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_crearActionPerformed(evt);
            }
        });

        jPanel1.add(bt_crear);
        bt_crear.setBounds(140, 150, 130, 23);

        jLabel6.setText("Codigo:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(190, 50, 60, 14);

        jPanel1.add(lbl_cod);
        lbl_cod.setBounds(230, 50, 34, 14);

        bt_borrar.setText("Borrar");
        bt_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_borrarActionPerformed(evt);
            }
        });

        jPanel1.add(bt_borrar);
        bt_borrar.setBounds(30, 150, 100, 23);

        jLabel3.setText("Observaciones:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(30, 110, 80, 20);

        jPanel1.add(txt_obs);
        txt_obs.setBounds(110, 110, 160, 20);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_crearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_crearActionPerformed
// TODO add your handling code here:
        accionBoton();
    }//GEN-LAST:event_bt_crearActionPerformed

    private void bt_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_borrarActionPerformed
// TODO add your handling code here:
        String codigo=lbl_cod.getText();
        //Realizo la moficacion del Cliente si es el caso que existe:
        // System.out.println("Aplasto Modificar");
        int opccion=0;
        opccion=JOptionPane.showConfirmDialog(this,"Seguro Desea Borrar este Usuario","Alerta",0);
        if(opccion==0){
            conexion.actualizarBase("delete from bodega where cod_bodega="+codigo+";");
            //cierro ventana
            uadmin.refrescar();
            this.dispose();
            
            
        }
    }//GEN-LAST:event_bt_borrarActionPerformed
    public void nuevoCodigo(){
        try{
            ResultSet r=conexion.consultarBase("select MAX(cod_bodega) from bodega");
            r.next();
            int numero=r.getInt(1)+1;
            System.out.println("Numero Factura Generado: "+numero);
            lbl_cod.setText(String.valueOf(numero));
         } catch(java.sql.SQLException e){};
    }
    
    public void cargarDatos(int fila){
    String codigo=uadmin.tabla.getValueAt(fila,0).toString();    
    lbl_cod.setText(codigo);
    ResultSet r=conexion.consultarBase("select * from bodega where cod_bodega="+codigo+";");
   
    try {
        r.next();
        txt_nom.setText(r.getString(2));
        txt_obs.setText(r.getString(3));
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    
    }    
    public void accionBoton(){
    
        //Valido que llenen todos los datos:
        if(txt_nom.getText().equals("")) {
            JOptionPane.showMessageDialog(this,"Los datos no han sido llenados correctamente","Informacion",JOptionPane.INFORMATION_MESSAGE);
        }else{
            //Tomo los Datos para la insercion:
            String priv;
            String nombre=txt_nom.getText();
            nombre=nombre.replaceAll(" ","");
            
            String observacion=txt_obs.getText();
            
            
            String codigo=lbl_cod.getText();
            
            //Realizo la insercion del Cliente si es el caso:
            
            if(bt_crear.getText().equals("Insertar"))
            {
               
                conexion.insertarDatos("insert into bodega values(null,'"+nombre+"','"+observacion+"')");
                
                //cierro ventana
                uadmin.refrescar();
                this.dispose();
            }
            else if(bt_crear.getText().equals("Modificar")){
           //Realizo la moficacion del Cliente si es el caso que existe:
               // System.out.println("Aplasto Modificar");
                 conexion.actualizarBase("update bodega set nombre='"+nombre+"', observaciones='"+observacion+"'"+ 
                                        " where cod_bodega="+codigo+";");
               //cierro ventana
                uadmin.refrescar();
                this.dispose();  
            }
                
                
            
            
            
            
        }
    
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_borrar;
    private javax.swing.JButton bt_crear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbl_cod;
    private javax.swing.JTextField txt_nom;
    private javax.swing.JTextField txt_obs;
    // End of variables declaration//GEN-END:variables
    
}
